home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1833
/
1833.xpi
/
chrome
/
yoono.jar
/
content
/
yoono
/
bookmarks
/
bookmarksxbel.js
< prev
next >
Wrap
Text File
|
2009-12-16
|
9KB
|
307 lines
/*
* bookmarksxbel.js
*
* @author david marteau <marteau.david@free.fr>
* @copyright 2005-2006 Yoono
*/
function ynXBELIOService()
{
this._bmUtils = new ynBookmarksUtils();
this._yns = new Namespace(YOONO_NS);
this._owner = "firefox-yoono-extension";
this._allowDuplicateURL = this._bmUtils.getPref("extensions.yoono.import.allowDuplicateURL",false);
}
ynXBELIOService.prototype =
{
/*
* import xbel xmlObject in 'root' folder
*/
_import : function( xbel, root )
{
var foldersets=[[xbel,root]];
var folderset=null;
var bmUtils = this._bmUtils;
var yns = this._yns;
var owner = this._owner;
var duplicate = this._allowDuplicateURL;
const urlRes = bmUtils.getArc("URL");
const nameRes = bmUtils.getArc("Name");
while(foldersets.length>0)
{
folderset=foldersets.shift();
var folder = folderset[0];
var parent = folderset[1];
var parentContainer = bmUtils.getContainer(parent);
var numItems = 0;
for each (var element in folder.*)
{
var res = null;
try {
switch(element.localName().toUpperCase())
{
case "BOOKMARK": {
if(!duplicate)
{
if(bmUtils.isBookmarked(urlRes,element.@href.toString()))
break;
}
var href = element.@href.toString();
var info = element..metadata.(@owner == owner);
// Check for live bookmarks
if( info && info.@yns::livemark.toString() == "true" )
{
var feedurl = info.@yns::feedurl.toString();
res = bmUtils.createLivemark(
element.title.toString(),href,feedurl,
element.desc.toString(),
null);
}
else
{
var shortcutUrl = info ? info.@yns::keywords.toString() : "";
res = bmUtils.createBookmark(
element.title.toString(),href,shortcutUrl,
element.desc.toString(),
null);
}
numItems++;
}
break;
case "FOLDER": {
// Check for toolbarfolder
var info = element..metadata.(@owner == owner);
if(info && info.@yns::toolbarfolder.toString() == "true")
{
foldersets.push([element,bmUtils.getToolBarFolder()]);
}
else
{
var title = element.title.toString();
if(!duplicate) {
var foundRes = bmUtils.getBookmarkedResource(nameRes,title);
if(foundRes) {
foldersets.push([element,foundRes]);
break;
}
}
var description = element.desc.toString();
if(description) {
/* XXX Need to set parent for setting description */
res = bmUtils.createFolder(title,description,parent);
foldersets.push([element,res]);
} else {
res = bmUtils.createFolder(title,null,null);
foldersets.push([element,res]);
}
numItems++;
}
}
break;
case "SEPARATOR":
// Add seprator only if items have been inserted
// before. This prevent useless separators to be
// displayed.
if(numItems>0)
res = bmUtils.createSeparator();
break;
}
if(res) {
var index = parentContainer.IndexOf(res);
if(index == -1)
parentContainer.AppendElement(res);
}
}
catch(exc)
{
// bmUtils.error("yoono:xbel",exc);
throw(exc);
}
}
}
},
/*
* export 'root' folder to xbel xmlObject
*/
_export : function( xbel, root )
{
var bmUtils = this._bmUtils;
var yns = this._yns;
var owner = this._owner;
const RDF = bmUtils.RDF;
const RDFC = bmUtils.RDFC;
const nsIRDFResource = Components.interfaces.nsIRDFResource;
const urlRes = bmUtils.getArc("URL");
const nameRes = bmUtils.getArc("Name");
const descRes = bmUtils.getArc("Description");
const keywordRes = bmUtils.getArc("ShortcutURL");
const iconRes = bmUtils.getArc("Icon");
const feedRes = bmUtils.getArc("FeedURL");
const dateRes = bmUtils.getArc("BookmarkAddDate");
var ds = bmUtils.BKMS;
var foldersets = [[xbel,root]];
var folderset = null;
var nodetree = [];
while(foldersets.length>0)
{
folderset = foldersets.shift();
var folder = folderset[0];
var parent = folderset[1];
var parentContainer = bmUtils.getContainer(parent);
var elements = parentContainer.GetElements();
var subtree = [];
while (elements.hasMoreElements())
{
var res = elements.getNext().QueryInterface(nsIRDFResource);
var separator = false;
var isFolder = false;
var element = null;
var info = null;
var type = bmUtils.getType(res);
switch(type)
{
case "Bookmark": {
var url = bmUtils.getProperty(res,urlRes);
var keywords = bmUtils.getProperty(res,keywordRes);
element = <bookmark href={url}/>;
if(keywords) {
info = <metadata owner={owner} xmlns:yns={YOONO_NS}
yns:keywords={keywords}/>;
}
}
break;
case "Livemark": {
var url = bmUtils.getProperty(res,urlRes);
var feedurl = bmUtils.getProperty(res,feedRes);
element = <bookmark href={url}/>;
info = <metadata owner={owner} xmlns:yns={YOONO_NS}
yns:livemark="true"
yns:feedurl={feedurl}/>;
}
break;
case "Folder":
element = <folder/>;
foldersets.push([element,res]);
break;
case "ToolbarFolder":
element = <folder/>;
info = <metadata owner={owner} xmlns:yns={YOONO_NS}
yns:toolbarfolder="true"/>;
foldersets.push([element,res]);
break;
case "BookmarkSeparator":
element = <separator/>;
separator = true;
break;
}
if(element)
{
if(!separator)
{
var name = bmUtils.getProperty(res,nameRes);
var desc = bmUtils.getProperty(res,descRes);
if(name) element.* += <title>{name}</title>;
if(desc) element.* += <desc>{desc}</desc>;
if(info) element.* += <info>{info}</info>;
}
subtree.push([element,folder]);
}
}
nodetree = nodetree.concat(subtree.reverse());
}
while(nodetree.length>0)
{
var node = nodetree.pop();
node[1].* += node[0];
}
return xbel;
},
/*
* Export bookmarks to XBEL format
*/
exportBookmarks : function ()
{
var bmUtils = this._bmUtils;
var xbel = <xbel version="1.0" xmlns:yns={YOONO_NS}/>;
var bmRoot = bmUtils.getRoot();
var title = bmUtils.getPropertyByName(bmRoot,"Name");
var desc = bmUtils.getPropertyByName(bmRoot,"Description");
if(title) xbel.* += <title>{title}</title>;
if(desc) xbel.* += <desc>{desc}</desc>;
xbel = this._export(xbel,bmRoot);
return xbel;
},
/*
* Import bookmarks from XBEL format
*/
importBookmarks : function ( xbel, aBookmarkFolder )
{
if(!aBookmarkFolder)
aBookmarkFolder = this._bmUtils.getRoot();
this._bmUtils.beginUpdateBatch();
var except;
try {
this._import(xbel,aBookmarkFolder);
} catch(ex) {
except = ex;
}
this._bmUtils.flush();
this._bmUtils.endUpdateBatch();
if(except)
throw except;
}
};